package com.hui.service;

import com.hui.entity.Course;
import com.hui.entity.CourseVo;
import com.hui.entity.Teacher;
import com.hui.mapper.CourseMapper;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: 李昭辉
 * @Date: 2021/08/05/10:08
 * @Description:
 */
@Service("courseService")
public class CourseServiceImpl implements CourseService {
    @Autowired
    private CourseMapper courseMapper;

    // 条件查询Course信息
    @Override
    public List<Course> getCourseByCondition(CourseVo courseVo) {
        return courseMapper.getCourseByCondition(courseVo);
    }

    // 新增Course信息，并新增Teacher信息
    @Override
    public void saveCourse(CourseVo courseVo) throws InvocationTargetException, IllegalAccessException {
        // 封装Course
        Course course = new Course();
        BeanUtils.copyProperties(course, courseVo);
        // 补全Course
        Date date = new Date();
        course.setCreateTime(date);
        course.setUpdateTime(date);
        // 新增Course
        courseMapper.saveCourse(course);
        // 封装Teacher
        Teacher teacher = new Teacher();
        BeanUtils.copyProperties(teacher, courseVo);
        // 补全teacher信息
        teacher.setCourseId(course.getId());
        teacher.setCreateTime(date);
        teacher.setUpdateTime(date);
        // 新增Teacher
        courseMapper.saveTeacher(teacher);
    }

    // 根据id查询Course详情信息
    @Override
    public CourseVo getCourseContentById(Integer id) {
        return courseMapper.getCourseContentById(id);
    }

    // 修改Course信息，并修改Teacher信息
    @Override
    public void updateCourse(CourseVo courseVo) throws InvocationTargetException, IllegalAccessException {
        // 封装course
        Course course = new Course();
        BeanUtils.copyProperties(course,courseVo);
        // 补全course
        Date date = new Date();
        course.setUpdateTime(date);
        // 执行修改操作
        courseMapper.updateCourse(course);
        // 封装teacher
        Teacher teacher = new Teacher();
        BeanUtils.copyProperties(teacher,courseVo);
        // 补全teacher
        teacher.setCourseId(course.getId());
        teacher.setUpdateTime(date);
        // 执行修改操作
        courseMapper.updateTeacher(teacher);
    }

    // 修改course状态
    @Override
    public void updateStatus(int status,int id) {
        Course course = new Course();
        // 封装数据
        course.setStatus(status);
        course.setId(id);
        course.setUpdateTime(new Date());
        courseMapper.updateStatus(course);
    }
}
